﻿package view.mornExtends
{
    import flash.display.*;
    import morn.core.components.*;

    public class FlashS9Bg extends Component implements ISkinAbleContainer
    {
        private var _skin:String;
        private var _minWidth:int;
        private var _minHeight:int;
        private var _firseWidth:int;
        private var _firseHeight:int;
        private var _skinChanged:Boolean = false;

        public function FlashS9Bg(param1:String = null)
        {
            if (param1 != null)
            {
                this.skin = param1;
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.skin = param1 as String;
            }
            return;
        }// end function

        public function set skin(param1:String) : void
        {
            if (param1 && this._skin != param1)
            {
                this._skin = param1;
                this.show();
                this.checkDraw();
            }
            this._skinChanged = true;
            return;
        }// end function

        private function show() : void
        {
            var _loc_1:* = App.asset.getAsset(this._skin);
            var _loc_2:* = App.asset.getAsset(this._skin + "$3");
            var _loc_3:* = App.asset.getAsset(this._skin + "$7");
            this._minWidth = _loc_1.width + _loc_2.width;
            this._minHeight = _loc_1.height + _loc_3.height;
            _width = _width > this._minWidth ? (_width) : (this._minWidth);
            _height = _height > this._minHeight ? (_height) : (this._minHeight);
            this._firseWidth = _loc_1.width;
            this._firseHeight = _loc_1.height;
            return;
        }// end function

        private function checkDraw() : void
        {
            this.clearBg();
            if (_width > this._minWidth)
            {
                if (_height > this._minHeight)
                {
                    this.drawCenter();
                }
                else
                {
                    this.drawWidth();
                }
            }
            else if (_height > this._minHeight)
            {
                this.drawHeight();
            }
            this.drawBase();
            return;
        }// end function

        private function clearBg() : void
        {
            while (numChildren > 0)
            {
                
                removeChildAt(0);
            }
            return;
        }// end function

        private function drawBase() : void
        {
            var _loc_1:* = App.asset.getAsset(this._skin);
            var _loc_2:* = App.asset.getAsset(this._skin + "$3");
            var _loc_3:* = App.asset.getAsset(this._skin + "$7");
            var _loc_4:* = App.asset.getAsset(this._skin + "$9");
            this.addAndSet(_loc_1.clone(), 0, 0);
            this.addAndSet(_loc_2.clone(), _width - _loc_2.width, 0);
            this.addAndSet(_loc_3.clone(), 0, _height - _loc_3.height);
            this.addAndSet(_loc_4.clone(), _width - _loc_2.width, _height - _loc_3.height);
            return;
        }// end function

        private function drawWidth() : void
        {
            var _loc_1:* = App.asset.getAsset(this._skin + "$2");
            var _loc_2:* = App.asset.getAsset(this._skin + "$8");
            var _loc_3:* = Math.ceil((_width - this._minWidth) / _loc_1.width);
            var _loc_4:* = _loc_1.width;
            var _loc_5:* = _loc_2.width;
            var _loc_6:* = _height - _loc_2.height;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_3)
            {
                
                this.addAndSet(_loc_1.clone(), this._firseWidth + _loc_7 * _loc_4, 0);
                this.addAndSet(_loc_2.clone(), this._firseWidth + _loc_7 * _loc_5, _loc_6);
                _loc_7++;
            }
            return;
        }// end function

        private function drawHeight() : void
        {
            var _loc_1:* = App.asset.getAsset(this._skin + "$4");
            var _loc_2:* = App.asset.getAsset(this._skin + "$6");
            var _loc_3:* = Math.ceil((_height - this._minHeight) / _loc_1.height);
            var _loc_4:* = _loc_1.height;
            var _loc_5:* = _width - _loc_2.width;
            var _loc_6:* = _loc_2.height;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_3)
            {
                
                this.addAndSet(_loc_1.clone(), 0, this._firseHeight + _loc_7 * _loc_4);
                this.addAndSet(_loc_2.clone(), _loc_5, this._firseHeight + _loc_7 * _loc_6);
                _loc_7++;
            }
            return;
        }// end function

        private function drawCenter() : void
        {
            var _loc_7:int = 0;
            var _loc_1:* = App.asset.getAsset(this._skin + "$5");
            var _loc_2:* = Math.ceil((_width - this._minWidth) / _loc_1.width);
            var _loc_3:* = Math.ceil((_height - this._minHeight) / _loc_1.height);
            var _loc_4:* = _loc_1.width;
            var _loc_5:* = _loc_1.height;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3)
            {
                
                _loc_7 = 0;
                while (_loc_7 < _loc_2)
                {
                    
                    this.addAndSet(_loc_1.clone(), this._firseWidth + _loc_7 * _loc_4, this._firseHeight + _loc_6 * _loc_5);
                    _loc_7++;
                }
                _loc_6++;
            }
            this.drawWidth();
            this.drawHeight();
            return;
        }// end function

        public function addAndSet(param1:BitmapData, param2:int, param3:int) : void
        {
            var _loc_4:Bitmap = null;
            var _loc_5:* = new Bitmap(param1.clone());
            _loc_4 = new Bitmap(param1.clone());
            this.addChild(_loc_5);
            _loc_4.x = param2;
            _loc_4.y = param3;
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            if (_width != param1)
            {
                _width = param1;
                callLater(changeSize);
                this.checkDraw();
            }
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            if (_height != param1)
            {
                _height = param1;
                callLater(changeSize);
                this.checkDraw();
            }
            return;
        }// end function

        public function get skin() : String
        {
            return this._skin;
        }// end function

    }
}
